package rabbit.topic;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/7/31 14:56
 */


public class UserConsumer {
    private static final String EXCHANGE_NAME = "outerhostexchange";// "outerhostexchange";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("39.106.17.245");
        factory.setHost("39.106.144.161");
        //factory.setHost("192.168.0.201");//"03156721261"
        factory.setVirtualHost("OuterHost");
        factory.setUsername("root");
        factory.setPassword("root");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true);
        String queueName = "organizationQueue";//"q.salarySystem";//"organizationQueue";// "eallqueue";//"housequeue";
        //channel.queueBind(queueName, EXCHANGE_NAME, "houseTopic");//"eallTopic");//"houseTopic");

        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
                //message=RSAHelper.decryptionByPrivateKey(message,"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIKPrL5IHCgm+fnzg8wpVS7D+1zwhVuuFdHcmUBhYUyll91Lqxtyk3XaeYP//B+X1KqEJuK9m49ZuFpq3Ygs1hiNu2MG6uUuonue7wBU8awxTzSUnUmv+rV8lNjIRVf11dLjkx7+XAa1rnwO/48pGyuT0QwTtrMCGLXIC1q9+1uHAgMBAAECgYAdF3yLTbAQdlbfDhbBCDi12e+JjwJ4Ijs0bu4q/Ufb9032fmTbEdWvvgWzlveL5p35fMbZYRxnfbtpUkSYkj0GG/A0lNIMQqD0i5dmy0rybP6/sUclZjYiHP8+yRRTM/20MrC0Rl//0jkK2jhzY4BDXRpuZgGcVF5pMTGxhLiYMQJBAMNAkI4qaLmPGLvQa8NcmozZnJdQgnfVskak/bMpYyn+WQlpwFzavPv6ma3x6MVPA9G+8B6v+ea+K4SZ4IFwn7kCQQCrLpt6RBrVt+iH/wA6z2X3QObt8iaCdxXX7mwOIUxSO7aAVhdHnuBTyCp6YTmzYAvTShfh4fSWtC6VyWYRqPU/AkEAi0QNF2oGYnw/nw7NHn55ugcpWwBQ3tqsmxjp4BWgJn7F0SH90/0tGOmpHdnTNpDiX4O6T5kjPULdVWpQKONE+QJAGPh7haJZp8NkEDKKfgmy/hZhmWQxaealMRJ+gLEqyPQc8yubyb7LSm1whNmZ6SihkPjF8xVa8IDoKsFuOjVDUQJAJXJd9MjEX9V7Bh0A7usro3KYnGMTz+r4gyDwgnbxHAncVnBwplvQPPC8JafzGpMYhal+Ej3u0kr32Cr+w/dadg==");
                //System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
            }
        };
        channel.basicConsume(queueName, true, consumer);
    }
}